﻿using System;
using System.Reflection;

namespace Utility.Extensions
{
    public static partial class Extensions
    {
        /// <summary>
        /// 获取方法定义的全名
        /// </summary>
        /// <param name="method">方法对象</param>
        /// <returns>方法全名，包含方法所在类的名称，如:xxNamespace.xxClass.xxMethod</returns>
        public static string GetFullName(this MethodInfo method)
        {
            if (method == null)
            {
                return string.Empty;
            }

            return string.Format("{0}#{1}", method.DeclaringType.FullName, method.Name);
        }

        /// <summary>
        /// Verifies the type of the paramter.
        /// </summary>
        /// <param name="methodInfo">The method information.</param>
        /// <param name="types">The types.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public static bool VerifyParameterType(this MethodInfo methodInfo, Type[] types)
        {
            var methodParameters = methodInfo.GetParameters();
            if ((methodParameters == null || methodParameters.Length == 0)
                && (types == null || types.Length == 0))
                return true;

            if ((methodParameters == null && types != null)
                || (methodParameters != null && types == null))
                return false;

            if (methodParameters.Length != types.Length)
                return false;
            for (var i = 0; i < methodParameters.Length; i++)
            {
                if (methodParameters[i].ParameterType != types[i])
                    return false;
            }
            return true;
        }
    }
}
